<template>
  <el-row :gutter="20">
    <el-col :span="6">
      <info-category @tree-node-click="treeNodeClick" />
    </el-col>
    <el-col :span="18">
      <div class="attr_value_page_header" v-if="showSign == 1">
        <el-page-header  @back="goBack" :content="attrName+'=可选值'"></el-page-header>
      </div>
      <infoAttrSelect v-if="showSign == 1" :attrId="attrId" :attrName="attrName"></infoAttrSelect>
      <div class="attr_name_title" v-show="showSign === 0">
        <span>{{catName}}</span>
      </div>
      <div class="mod-config" v-show="showSign == 0">
        <el-form
          :inline="true"
          :model="dataForm"
          @keyup.enter.native="getDataList()"
        >
          <el-form-item>
            <el-input
              v-model="dataForm.key"
              placeholder="参数名"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="getDataList()">查询</el-button>
            <el-button
              v-if="isAuth('admin:infoattr:save')"
              type="primary"
              @click="addOrUpdateHandle()"
              >新增</el-button
            >
            <el-button
              v-if="isAuth('admin:infoattr:delete')"
              type="danger"
              @click="deleteHandle()"
              :disabled="dataListSelections.length <= 0"
              >批量删除</el-button
            >
            <el-button type="primary" @click="refreshHandle()">刷新</el-button>
          </el-form-item>
        </el-form>
        <el-table
          :data="dataList"
          border
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandle"
          style="width: 100%"
        >
          <el-table-column
            type="selection"
            header-align="center"
            align="center"
            width="50"
          >
          </el-table-column>
          <el-table-column
            prop="attrId"
            header-align="center"
            align="center"
            label="属性id"
          >
          </el-table-column>
          <el-table-column
            prop="attrName"
            header-align="center"
            align="center"
            label="属性名"
          >
          </el-table-column>
          <el-table-column
            prop="catId"
            header-align="center"
            align="center"
            label="分类id"
          >
          </el-table-column>
          <el-table-column
            prop="type"
            header-align="center"
            align="center"
            label="输入类型"
          >
          </el-table-column>
          <el-table-column
            prop="searchType"
            header-align="center"
            align="center"
            label="检索字段"
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.searchType"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
                @change="handlerSwitchChange(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column
            prop="showStatus"
            header-align="center"
            align="center"
            label="是否显示"
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.showStatus"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
                @change="handlerShowStatusChange(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <!-- <el-table-column
            prop="valueSelect"
            header-align="center"
            align="center"
            label="可选值列表[用分号分隔]"
          >
            <template slot-scope="scope">
              <el-tooltip placement="top">
                <div slot="content">
                  <span v-for="(i, index) in scope.row.valueSelect.split(';')"
                    :key="index" >{{ i }}<br/>
                  </span>
                </div>
                <el-tag>
                  {{ scope.row.valueSelect.split(";")[0] + " ..." }}
                </el-tag>
              </el-tooltip>
            </template>
          </el-table-column> -->
          <el-table-column
            prop="sort"
            header-align="center"
            align="center"
            label="排序"
          >
          </el-table-column>
          <el-table-column
            prop="icon"
            header-align="center"
            align="center"
            label="属性图标"
          >
          </el-table-column>
          <el-table-column
            fixed="right"
            header-align="center"
            align="center"
            width="150"
            label="操作"
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                @click="showAttrSelect(scope.row)"
                >可选值</el-button
              >
              <el-button
                type="text"
                size="small"
                @click="addOrUpdateHandle(scope.row.attrId)"
                >修改</el-button
              >
              <el-button
                type="text"
                size="small"
                @click="deleteHandle(scope.row.attrId)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update
          v-if="addOrUpdateVisible"
          ref="addOrUpdate"
          :catIdPath='catIdPath'
          @refreshDataList="getDataList"
        ></add-or-update>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import InfoCategory from "../common/info-category.vue";
import AddOrUpdate from "./attr-add-or-update";
import InfoAttrSelect from './infoattrselect';
export default {
  data() {
    return {
      showSign: 0, // 0表示attr，1表示attrSelect 
      attrId: 0,
      attrName: '',
      catId: 0,
      catIdPath: [],
      catName: '信息种类',
      dataForm: {
        key: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
    };
  },
  components: {
    AddOrUpdate,
    InfoCategory,
    InfoAttrSelect,
  },
  activated() {
    // this.getDataList();
    this.dataList=[
      {
        attrId:1,
        attrName: '物品名',
        catId: 5,
        searchType: 0,
        typeId: 1,
        inputType: 'input',
        icon: 'icon123',
        valueSelect:'',
      },{
        attrId:2,
        attrName: '时间',
        catId: 5,
        searchType: 0,
        typeId: 1,
        inputType: 'datetime',
        icon: 'icon234',
        valueSelect:'data;time',
      }
    ]
  },
  methods: {
    // 感知 树中 节点被点击，并进行 查找
    treeNodeClick(data, node, component) {
      console.log("父组件中感知到", data, node);
      console.log("被点击的节点id:", data.catId);

      //进行查找
      if (data.catLevel > 1) {
        this.catId = data.catId;
        this.catName = data.catName;
        this.getDataList();
      }
    },

    //检测 switch 的变动，并请求修改
    handlerSwitchChange(row) {
      console.log("switch最新信息", row);
      var { attrId, searchType } = row;
      this.$http({
        url: this.$http.adornUrl("/admin/info/attr/update"),
        method: "post",
        data: this.$http.adornData(
          { attrId: attrId, searchType: searchType ? 1 : 0 },
          false
        ),
      }).then(({ data }) => {
        this.$message({
          type: "success",
          message: "修改状态成功!",
        });
      });
    },

    // 刷新表格数据
    refreshHandle() {
      this.getDataList();
    },

    // 获取数据列表
    getDataList() {
      console.log("获取", this.catId, "对应的属性" )
      this.dataListLoading = true;
      var data = {
        key:this.dataForm.key,
        page:''+this.pageIndex,
        limit:''+this.pageSize,
        sidx: 'sort',
        order:'asc'
      }
      this.$http({
        url: this.$http.adornUrl(`/admin/info/attr/list/vo/${this.catId}`),
        method: "post",
        data: this.$http.adornData(data, false),
      }).then(({ data }) => {
        console.log("成功从后台获取到数据");
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
          this.catIdPath = data.catIdPath;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      // console.log("新增 or 修改 时的id111",id)
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.attrId;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/admin/info/attr/delete"),
          method: "post",
          data: this.$http.adornData(ids, false),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    // 修改 showStatus
    handlerShowStatusChange(row){
      this.$http({
        url: this.$http.adornUrl(`/admin/info/attr/update/${row.attrId}/${row.showStatus}`),
        method: "post",
      }).then(({ data }) => {
        this.$message({
          type: "success",
          message: "修改状态成功!",
        });
        
      });
    },
    // goBack
    goBack(){
      console.log('goBack')
      this.showSign = 0;
    },
    // 显示某个属性的可选值
    showAttrSelect(row){
      console.log("查看可选值，attrId：",row.attrId)
      this.attrId = row.attrId;
      this.attrName = row.attrName;
      this.showSign = 1;
    }
  },
};
</script>
<style>
.attr_value_page_header{
  margin-bottom: 10px;
}
.attr_name_title{
  height: 24px;
  margin-bottom: 10px;
  font-size: 18px;
}
</style>